/**
 * author: zhangwei
 * 角色新增、编辑
 */
var pageType = "add";
var userType = '${userType}'
var main = {

    init: function() {
        loadData();
        this.initTable();
    },
    initTable: function() {
        $('#btnAdd').click(function() {
            var json = {"id":$("#id").val()}
            $.ajax({
                type:"get",
                data:json,
                async:false,
                url:"/plan/joinPlan",
                success:function(result) {
                    if (result.code == property.successCode) {
                        successMsg("已加入")
                        $("#actionBtn").hide()
                        // form.render('select');
                    } else {
                        errorMsg(result.msg);
                    }
                },
                error:function(result) {
                    errorMsg("系统异常");
                }
            });
        });
    }
}
main.init();

/**
 * 加载表单数据
 * @param id 角色id
 */
function loadData() {
    layui.use('form', function(){
        var form = layui.form;
        var index = parent.layer.getFrameIndex(window.name);
        //加载数据
        $.ajax({
            type:"get",
            async:false,
            url:"/plan/getMyPlan",
            success:function(result) {
                if (result.code == property.successCode) {
                    if (result.data == null){
                        alertMsg("请先在计划列表中选择计划加入")
                    }else{
                        setFormData(result.data);
                    }
                    // form.render('select');
                } else {
                    errorMsg(result.msg);
                }
            },
            error:function(result) {
                errorMsg("系统异常");
            }
        });
    });
}


/**
 * 设置表单数据
 * @param data
 */
function setFormData(data) {
    data.startTime = formatSimpleDate(data.startTime)
    data.endTime = formatSimpleDate(data.endTime)
    $("#id").val(data.id)
    $("#planDesc").text(data.planDesc)
    $("#planName").text(data.planName)
    $("#evaluationCriterion").text(data.evaluationCriterion)
    $("#startTime").text(data.startTime)
    $("#endTime").text(data.endTime)
    $("#joinPeopleStr").text(data.joinPeopleStr)
    var statusStr = data.statusStr
    var userType = data.userType
    var hasJoin = data.hasJoin
    if(userType != 'student' || statusStr != '进行中'||hasJoin==true){
        $("#actionBtn").hide()
    }else{
        $("#actionBtn").show()
    }
    if (data.evaluate != null){
        $("#evaluate").text(data.evaluate)
    }
    if (data.score != null){
        $("#score").text(data.score)
    }


}



